package
{
	import com.karst.webnotes.chapter02.Chapter02Entry;
	import com.karst.webnotes.chapter03.Chapter03Entry;
	import com.karst.webnotes.chapter04.Chapter04Entry;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.aswing.AsWingManager;
	import org.aswing.CenterLayout;
	import org.aswing.Container;
	import org.aswing.FlowLayout;
	import org.aswing.JButton;
	import org.aswing.JComboBox;
	import org.aswing.JWindow;
	
	public class WebNotesStudy extends Sprite
	{
		public function WebNotesStudy()
		{
			initStage() ;
			createView() ;
		}

		private function initStage() : void
		{
			AsWingManager.initAsStandard( this ) ;
		}

		private function createView() : void
		{
			createWindow() ;
			createComb() ;
			createViewBtn() ;
			addEvents() ;
		}

		private var window : JWindow = null ;
		private var pane : Container = null ;
		private function createWindow() : void
		{
			if( window == null )
			{
				window = new JWindow( this ) ;
				window.setSizeWH( stage.stageWidth, stage.stageHeight ) ;
			}
			
			if( pane == null )
			{
				pane = window.getContentPane() ;
				pane.setLayout( new FlowLayout() ) ;
			}

			window.show() ;
		}

		private var chapterComb : JComboBox = null ;
		private var sectionComb : JComboBox = null ;
		private static const CHAPTER_LIST : Array = [ 2, 3, 4, 5] ;
		private function createComb() : void
		{
			if( chapterComb == null )
			{
				chapterComb = new JComboBox() ;
				chapterComb.setListData( CHAPTER_LIST ) ;
				chapterComb.setSelectedItem( CHAPTER_LIST[0] ) ;
			}
			if( sectionComb == null )
			{
				sectionComb = new JComboBox() ;
				sectionComb.setListData( Chapter02Entry.SECTION_LIST ) ;
				sectionComb.setSelectedItem( Chapter02Entry.SECTION_LIST[0] ) ;
			}
			pane.appendAll( chapterComb, sectionComb ) ;
		}

		private var viewBtn : JButton = null ;
		private function createViewBtn() : void
		{
			if( viewBtn == null )
				viewBtn = new JButton( "View" ) ;
			pane.append( viewBtn ) ;
		}


		
		//////////////////////////////////////////////////////////
		// chapter03-第一个应用
 		//////////////////////////////////////////////////////////
		private var chapter02 : Chapter02Entry = null ;
		private function createCh02View() : void
		{
			if( chapter02 == null )
				chapter02 = new Chapter02Entry() ;
			pane.addChild( chapter02 ) ;
			chapter02.showSection( sectionComb.getSelectedItem() ) ;
		}

		
		
		//////////////////////////////////////////////////////////
		// chapter03-几何体
 		//////////////////////////////////////////////////////////
		private var chapter03 : Chapter03Entry = null ;
		private function createCh03View() : void
		{
			if( chapter03 == null )
				chapter03 = new Chapter03Entry() ;
			pane.addChild( chapter03 ) ;
			chapter03.showSection( sectionComb.getSelectedItem() ) ;
		}

		
		
		//////////////////////////////////////////////////////////
		// chapter04-Materials材质
 		//////////////////////////////////////////////////////////
		private var chapter04 : Chapter04Entry = null ;
		private function createCh04View() : void
		{
			if( chapter04 == null )
				chapter04 = new Chapter04Entry() ;
			pane.addChild( chapter04 ) ;
			chapter04.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		
		private function addEvents() : void
		{
			AsWingManager.getStage().addEventListener( Event.RESIZE, stageResizeHandler ) ;
			if( chapterComb )
				chapterComb.addActionListener( selectChapterHandler ) ;
			if( viewBtn )
				viewBtn.addEventListener( MouseEvent.CLICK, viewHandler ) ;
		}
		
		private function stageResizeHandler( event : Event ) : void
		{
			window.setSizeWH( stage.stageWidth, stage.stageHeight ) ;
		}
		
		private function selectChapterHandler( event : Event ) : void
		{
			if( sectionComb == null )
				return ;
			switch( chapterComb.getSelectedItem() )
			{
				case CHAPTER_LIST[0]:
					sectionComb.setListData( Chapter02Entry.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[1]:
					sectionComb.setListData( Chapter03Entry.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[2]:
					sectionComb.setListData( Chapter04Entry.SECTION_LIST ) ;
					break;
				default:
					break;
			}
		}
		
		private  function viewHandler( event : MouseEvent = null ) : void
		{
			resetPaneView() ;
			switch( chapterComb.getSelectedItem() )
			{
				case CHAPTER_LIST[0]:
					createCh02View() ;
					break;
				case CHAPTER_LIST[1]:
					createCh03View() ;
					break;
				case CHAPTER_LIST[2]:
					createCh04View() ;
					break;
				default:
					break;
			}
		}

		private function resetPaneView() : void
		{
			pane.removeAll() ;
			while( pane.numChildren )
				pane.removeChildAt( 0 ) ;
			if( chapterComb )
				pane.appendAll( chapterComb, sectionComb, viewBtn ) ;
		}
	}
}